//
//  WJEssAndNewBelowVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// 这个是导航栏
#import "WJEssAndNewBelowVC.h"
#import "WJTopCollectionViewCell.h"
#import "WJMianController.h"

@interface WJEssAndNewBelowVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain) NSIndexPath *sliderPath;
@property (nonatomic,assign) NSInteger presentTag;
@end

@implementation WJEssAndNewBelowVC

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// 上下联动

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.presentTag = 1000;
    
    self.isThrough = NO;
    
    [self createTopView];
    
    [self createbelowView];
}

#pragma mark - 创建导航栏
- (void)createTopView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    self.topCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH , 40) collectionViewLayout:flowLayout];
    self.topCollectionView.delegate = self;
    self.topCollectionView.dataSource = self;
    self.topCollectionView.bounces = YES;
    self.topCollectionView.showsHorizontalScrollIndicator = NO;
    self.topCollectionView.backgroundColor = BSWhiteColor;
    [self.topCollectionView registerClass:[WJTopCollectionViewCell class] forCellWithReuseIdentifier:@"topCell"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTopColor) name:@"changeColor" object:nil];
    [self.view addSubview:self.topCollectionView];
    
    CALayer *line = CALayer.new;
    line.frame = CGRectMake(0, 39, WIDTH, 1);
    line.backgroundColor = BSLightGrayColor.CGColor;
    [self.view.layer addSublayer:line];
        
    [self changeTopColor];
}

- (void)changeTopColor {
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
        self.topCollectionView.backgroundColor = FORBLACKCOLOR;
    }else{
        self.topCollectionView.backgroundColor = BSWhiteColor;
    }
}



#pragma mark - 创建滑动UICollectionView

- (void)createbelowView {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.belowCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, WIDTH , BSViewH - 40 - BSTabBarH) collectionViewLayout:flow];
    self.belowCollectionView.backgroundColor = BSWhiteColor;
    self.belowCollectionView.pagingEnabled = YES;
    self.belowCollectionView.bounces = YES;
    self.belowCollectionView.delegate = self;
    self.belowCollectionView.dataSource = self;
    self.belowCollectionView.showsHorizontalScrollIndicator = NO;
    [self.belowCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"belowCell"];
    [self.view addSubview:self.belowCollectionView];
}

- (void)setIsThrough:(BOOL)isThrough {
    _isThrough = isThrough;
    self.belowCollectionView.height = BSViewH - 40;
}


// cell大小的自适应
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.topCollectionView]) {
        
        WJMianController *vc = self.childViewControllers[indexPath.row];
        
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [vc.title boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return CGSizeMake(rect.size.width + 20, 40);
    }
    
    if (self.isThrough) {
        return CGSizeMake(WIDTH, BSViewH - 40);
    } else {
        return CGSizeMake(WIDTH, BSViewH - 40 - BSTabBarH);
    }
}

#pragma mark - CollectionView Delelgate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childViewControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.topCollectionView]) {  //上面的
        
        WJTopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topCell" forIndexPath:indexPath];
        
        // 给他赋值
        WJMianController *vc = self.childViewControllers[indexPath.row];
        cell.lablel.text = vc.title;
        cell.sliderLable.tag = 1000 + indexPath.row;
        
        // 第一次的时候显示UILable
        UILabel *lable = (UILabel *)[self.topCollectionView viewWithTag:self.presentTag];
        lable.hidden = NO;
        
        // 去除重用的BUG
        if (![indexPath isEqual:self.sliderPath]) {
            cell.sliderLable.hidden = YES;
        }
        return cell;
        
    }else{   //下面的
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"belowCell" forIndexPath:indexPath];
        
        // 先移除 子视图的View
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        WJMianController *subVC = self.childViewControllers[indexPath.row];
        if (self.isThrough) {
            subVC.view.frame = CGRectMake(0, 0, WIDTH, BSViewH - 40);
        } else {
            subVC.view.frame = CGRectMake(0, 0, WIDTH, BSViewH - 40 - BSTabBarH);
        }

        [cell.contentView addSubview:subVC.view];
        return cell;
    }
}

#pragma mark - UICollectionView delegate  点击上面的让下面的偏移量发生变化
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.topCollectionView]) {
        
        //  改变下面的偏移量
        self.belowCollectionView.contentOffset = CGPointMake(indexPath.row * WIDTH, 0);
        
        // 返回原来的状态
        UILabel *lable = (UILabel *)[self.topCollectionView viewWithTag:self.presentTag];
        lable.hidden = YES;
        
        // 改变UIlable的位置
        WJTopCollectionViewCell *cell1 = (WJTopCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell1.sliderLable.hidden = NO;
        self.presentTag = cell1.sliderLable.tag;
        self.sliderPath = indexPath;
        
        // 在中间显示
        [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
    }
}


#pragma mark - 滑动下面的改变上面的

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.belowCollectionView]) {
        CGFloat temp = self.belowCollectionView.contentOffset.x / WIDTH;
        // 回复
        UILabel *lable = (UILabel *)[self.topCollectionView viewWithTag:self.presentTag];
        lable.hidden = YES;
        
        // 发生变化
        UILabel *lable1 = (UILabel *)[self.topCollectionView viewWithTag:temp + 1000];
        lable1.hidden = NO;
        self.presentTag = temp +1000;
        
        // 在中间显示
        [self.topCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:temp inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
    }
}





@end
